package com.xiaochong.tir.manager.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.NonListedCompanyIndex;
import com.xiaochong.tir.manager.service.NonListedCompanyIndexService;
import com.xiaochong.tir.manager.service.SyAreaService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;

/**
 * 非上市公司指数管理
 * 
 * @author 杨松松
 *
 */
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX + "/index")
public class NonListedCompanyIndexController {
	@Autowired
	private NonListedCompanyIndexService nonListedCompanyIndexService;
	@Autowired
	private SyAreaService syAreaService;
	@Autowired
	private RedisServer redisServer;
	/**
	 * 非上市公司指数列表页
	 * @return
	 */
	@RequestMapping("/nonListedCompanyIndexList")
	public String nonListedCompanyIndexList() {
		return "/nonListedCompanyIndex/nonListedCompanyIndexList";
	}
	
	/**
	 * 非上市公司指数列表
	 * @param limit
	 * @param page
	 * @param nonListedCompanyIndex
	 * @return
	 */
	@RequestMapping("/listNonListedCompanyIndex")
	@ResponseBody
	public Object listNonListedCompanyIndex(@RequestParam("limit") int limit, @RequestParam("page") int page,
			NonListedCompanyIndex nonListedCompanyIndex) {
		//日志格式错误
		//LogTrace.info(getClass().getSimpleName() + ".listNonListedCompanyIndex()", "");
		try {
			PageInfo<NonListedCompanyIndex> pageInfo = nonListedCompanyIndexService.findList(nonListedCompanyIndex,
					page, limit);
			return new ReturnResult<NonListedCompanyIndex>(0, "查询成功！", pageInfo.getList(), pageInfo.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + ".listNonListedCompanyIndex()", e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 非上市公司指数添加页
	 * @param model
	 * @return
	 */
	@RequestMapping("/nonListedCompanyIndexAdd")
	public String nonListedCompanyIndexAdd(Model model) {
		// 查询所有城市
		List<Map<String,String>> cityList = syAreaService.getAllArea();
		model.addAttribute("cityList", cityList);
		return "/nonListedCompanyIndex/nonListedCompanyIndexAdd";
	}

	/**
	 * 新增非上市公司指数
	 * @param nonListedCompanyIndex
	 * @param session
	 * @return
	 */
	@RequestMapping("/saveNonListedCompanyIndex")
	@ResponseBody
	public Object saveNonListedCompanyIndex(NonListedCompanyIndex nonListedCompanyIndex, HttpSession session) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"新增非上市公司指数");
		try {
			NonListedCompanyIndex index = nonListedCompanyIndexService.selectByEntity(nonListedCompanyIndex);
			if(null != index){
				return new ReturnResult<NonListedCompanyIndex>(-1, "重复添加！", 1, 0);
			}
			nonListedCompanyIndex.setDeleteFlag(false);
			nonListedCompanyIndex.setCreateTime(new Date());
			nonListedCompanyIndex.setUpdateTime(new Date());
			nonListedCompanyIndexService.addNonListedCompanyIndex(nonListedCompanyIndex);
			return new ReturnResult<NonListedCompanyIndex>(0, "新增成功！", 1, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + ".saveNonListedCompanyIndex()", e.getMessage(), e);
			return new ReturnResult<NonListedCompanyIndex>(-1, "新增失败", 0, 0);
		}
	}

	/**非上市公司指数编辑页
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/nonListedCompanyIndexEdit")
	public String nonListedCompanyIndexEdit(@RequestParam("id") Integer id, Model model) {
		NonListedCompanyIndex nonListedCompanyIndex = nonListedCompanyIndexService.getNonListedCompanyIndex(id);
		model.addAttribute("data", nonListedCompanyIndex);
		// 查询所有城市
		List<Map<String,String>> cityList = syAreaService.getAllArea();
		model.addAttribute("cityList", cityList);
		return "/nonListedCompanyIndex/nonListedCompanyIndexEdit";
	}

	/**
	 * 更新非上市公司指数
	 * @param nonListedCompanyIndex
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateNonListedCompanyIndex")
	@ResponseBody
	public Object updateNonListedCompanyIndex(NonListedCompanyIndex nonListedCompanyIndex, HttpSession session) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"编辑非上市公司指数");
		try {
			nonListedCompanyIndex.setUpdateTime(new Date());
			nonListedCompanyIndexService.updateNonListedCompanyIndex(nonListedCompanyIndex,nonListedCompanyIndexService.selectByPrimaryKey(nonListedCompanyIndex.getId()));
			return new ReturnResult<NonListedCompanyIndex>(0, "更新成功！", 1, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + ".updateNonListedCompanyIndex()", e.getMessage(), e);
			return new ReturnResult<NonListedCompanyIndex>(-1, "保存失败", 0, 0);
		}
	}


	/**
	 * 更新非上市公司指数状态
	 * @param id
	 * @param enableFlag
	 * @param msg
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateEnableFlag")
	@ResponseBody
	public Object updateEnableFlag(@RequestParam("id") Integer id, @RequestParam("enableFlag") Boolean enableFlag,
			@RequestParam("msg") String msg, HttpSession session) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"非上市公司指数启用/停用");
		try {
			NonListedCompanyIndex nonListedCompanyIndex = new NonListedCompanyIndex();
			nonListedCompanyIndex.setId(id);
			nonListedCompanyIndex.setEnableFlag(enableFlag);
			nonListedCompanyIndex.setUpdateTime(new Date());
			int count = nonListedCompanyIndexService.updateEnableFlag(nonListedCompanyIndex,nonListedCompanyIndexService.selectByPrimaryKey(id));
			return new ReturnResult<NonListedCompanyIndex>(0, msg, count, 0);
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + ".updateEnableFlag()", e.getMessage(), e);
			return new ReturnResult<NonListedCompanyIndex>(-1, "保存失败", 0, 0);
		}
	}

}
